const express = require('express') 
const path = require('path')
const bodyParser = require('body-parser')
const multer = require('multer')
const app = express()

// let upload = multer({dest:'./data/imgs'})
let option = {
    storage:multer.diskStorage({
        destination: function (req, file, cb) {
          cb(null, './data/imgs')
        },
        filename: function (req, file, cb) {
          cb(null, Date.now()+""+Math.floor(Math.random()*10000)+path.extname(file.originalname))
        }
      }),
      fileFilter:function(req,file,cb){
          if(file.mimetype === 'image/jpeg'){
              cb(null,true)
          }else{
              cb(null,false)
          }
      },
      limits:{
        files:9,
        fileSize:1024*1024*10
      }
}
let upload = multer(option)
// 配置body-parser 中间件 解析传递的参数
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use('/s',express.static(path.join(__dirname,'./public')))

app.post('/upload/file',upload.array('myfile',9),(req,res)=>{
    console.log(req.file);
    console.log(req.files)
    res.send('上传成功')
})
let server = app.listen(3000,()=>{
    let address = server.address().address
    let port = server.address().port
    console.log(`server is running hostname is ${address} port is ${port}`);
})